1364A - XXXXX - CodeForces Solution


brute force data structures number theory two pointers *1200

Please click on ads to support us..

Python Code:

import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
from functools import lru_cache
import math
from collections import OrderedDict
from collections import Counter
from itertools import combinations
from itertools import accumulate
import operator
import bisect
from collections import deque,defaultdict
import copy
from itertools import permutations
from operator import itemgetter
from functools import lru_cache
 
 
BUFSIZE = 8192
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
stdin, stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) 
 
mod = pow(10, 9) + 7
mod2 = 998244353
        
def inp(): return stdin.readline().strip()
def iinp(): return int(inp())
def out(var, end="\n"): stdout.write(str(var)+"\n")
def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end)
def lmp(): return list(mp())
def mp(): return map(int, inp().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)]
def ceil(a, b): return (a+b-1)//b
 
def bin(a,b):
    if b==0:
        return 1
    r=bin(a,b//2)
    if b%2:
        return r*r*a
    else:
        return r*r
 
def gcd(a,b):
    if a == 0:
        return b
    return gcd(b % a, a)

def lcm(a,b):
    return (a // gcd(a,b))* b
    
def solve(n,a,x):
    d=sum(a)
    i=0
    while i<n and d%x==0:
        d-=a[i]
        i+=1
    l=n-1
    d=sum(a)
    while l>=0 and d%x==0:
        d-=a[l]
        l-=1
    e=max(l+1,n-i)
    if e:
        print(e)
    else:
        print(-1)
t=iinp()
for i in range(t):
        n,x=lmp()
    a=lmp()    solve(n,a,x)
'''
a=inp()
b=inp()
solve(a,b)
'''

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt","w",stdout);
    
    #endif
        int t;
        cin>>t;
        while(t--){
            int n,k,x,sum = 0,ans = -1;
            cin>>n>>k;
            int arr[n],dp[n] = {0};
            for(int i = 0;i<n;i++){
                cin>>arr[i];
                sum +=arr[i];
                if(sum%k != 0){
                    ans = max(ans,i+1);
                }
            }
           for(int i= 0;i<n;i++){
                sum -= arr[i];
                if(sum%k != 0){
                    ans = max(ans,n-i-1);
                }
           }
           int mid = (n-1)/2;
           for(int i = 0,j = n-1;i<mid,j>mid;i++,j--){
                sum = sum - arr[i]-arr[j];
                if(sum%k != 0){
                    ans = max(ans,j-i-1);
                }
           }
           cout<<ans<<endl;

        }
    return 0;
}


Comments

Submit
0 Comments
More Questions

189A - Cut Ribbon
1182A - Filling Shapes
82A - Double Cola
45A - Codecraft III
1242A - Tile Painting
1663E - Are You Safe
1663D - Is it rated - 3
1311A - Add Odd or Subtract Even
977F - Consecutive Subsequence
939A - Love Triangle
755A - PolandBall and Hypothesis
760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books
1374B - Multiply by 2 divide by 6
1093B - Letters Rearranging
1213C - Book Reading
1468C - Berpizza
1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci